<template>
  <div>
    <!-- <h3>form组件</h3> -->
    <el-dialog
      :title="status.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 树型控件 -->
        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="treeList"
            show-checkbox
            default-expand-all
            node-key="id"
            :props="{ children: 'children', label: 'title' }"
            ref="tree"
          >
          </el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isshow = status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  props: ["status", "list"],
  data() {
    return {
      // 添加接口初始化数据
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
      // 树型控件数据
      treeList: [],
    };
  },
  methods: {
    ...mapActions({
      changeInfo: "changeInfo",
    }),
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
      // 清空树
      this.$refs.tree.setCheckedKeys([]);
    },
    add() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空
          this.empty();
          //   弹框消失
          this.status.isshow = false;
          // 列表重新渲染
          this.$emit("init");
        }
      });
    },

    // 点击编辑之后
    getOne(id) {
      reqroleinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          // 补一个id,删除时候要用
          this.form.id = id;
          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
        }
      });
    },
    // 点击修改
    update() {
      // 取出树的值给form.menus
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);

          // 修改的角色是自己正在登录着的
          if (this.form.id == this.info.roleid) {
            // 清空数据
            this.changeInfo({});
            // 退出登录
            this.$router.replace("/login");
            return;
          }

          console.log(this.form.id);
          console.log(this.info.roleid);

          // 清空form及树
          this.empty();
          //   弹框消失
          this.status.isshow = false;
          // 列表重新渲染
          this.$emit("init");
        }
      });
    },
    closed() {
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
    // 取出树型控件数据
    reqmenulist({
      istree: true,
    }).then((res) => {
      this.treeList = res.data.list;
    });
  },
  computed: {
    ...mapGetters(["info"]),
  },
};
</script>

<style>
</style>